package com.xyyl.patient.service.order;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.util.DateUtils;
import com.xyyl.patient.entity.archives.Archives;
import com.xyyl.patient.entity.archives.ArchivesSnapshot;
import com.xyyl.patient.entity.order.PhoneOrder;
import com.xyyl.patient.entity.user.AccountRecord;
import com.xyyl.patient.entity.user.User;
import com.xyyl.patient.mapper.archives.ArchivesMapper;
import com.xyyl.patient.mapper.archives.ArchivesSnapshotMapper;
import com.xyyl.patient.mapper.order.PhoneOrderMapper;
import com.xyyl.patient.mapper.user.AccountRecordMapper;
import com.xyyl.patient.mapper.user.UserMapper;

/**
 * @projectName:xyyl-patient-service
 * @ClassName: PhoneOrderServiceImp
 * @Description:电话咨询订单业务实现
 * @author chen
 * @date 2017年8月24日
 *
 */
@Service("phoneOrderService")
public class PhoneOrderServiceImpl implements PhoneOrderService {

	@Autowired
	private PhoneOrderMapper phoneOrderMapper;

	@Autowired
	private AccountRecordMapper accountRecordMapper;

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private ArchivesMapper archivesMapper;
	
	@Autowired
	private ArchivesSnapshotMapper archivesSnapshotMapper;
	

	@Override
	public int insertConsultOrder(PhoneOrder consultOrder) {
		
		Integer archivesSnapshotId=null;
		if(consultOrder.getArchivesid()!=null){
			archivesSnapshotId=createArchivesSnapshot(consultOrder.getArchivesid());//档案快照ID
			consultOrder.setSnapshotid(archivesSnapshotId);
		}
		String preCode = DateUtils.formatDatetime(new Date(), "yyyyMMddHHss") + RandomUtils.nextInt(99999);
		consultOrder.setOrdersn("PO-"+preCode);
		consultOrder.setPayType(0);
		consultOrder.setCreatetime(DateUtils.currentDatetime());
		
		phoneOrderMapper.insert(consultOrder);

		return consultOrder.getId();
	}

	
	/**
	 * @Title: createArchivesSnapshot
	 * @Description: 创建档案快照
	 * @param @return    参数
	 * @return int    返回类型
	 * @throws
	 */
	public int createArchivesSnapshot(Integer archiveId ){
		
		/**获取原档案信息 **/
	   Archives archives=archivesMapper.selectOne(archiveId);
	   ArchivesSnapshot archivesSnapshot=null;
	   if(archives!=null){
		  archivesSnapshot=new ArchivesSnapshot();
		  BeanUtils.copyProperties(archives, archivesSnapshot);
		  archivesSnapshot.setId(null);
		  archivesSnapshotMapper.insert(archivesSnapshot);
	   }
	   
	   return archivesSnapshot.getId();
	}
	
	@Override
	public int updateConsultOrder(PhoneOrder consultOrder) {

		Integer payType = consultOrder.getPayType();

		/** 1.更新支付状态 **/
		consultOrder.setUpdatetime(DateUtils.currentDatetime());
		consultOrder.setPaytime(DateUtils.currentDatetime());
		consultOrder.setStatus(AppEnum.common.Pay_status_confirm.getValue());
		int result = phoneOrderMapper.update(consultOrder);

		/** 余额支付则需要更新相关信息 **/
		if (payType.equals(AppEnum.common.Order_payType_account.getValue())) {

			/** 2.查询账户信息 **/
			User user = userMapper.queryUserByOption(null, consultOrder.getOpenid());
			BigDecimal beforeBalance = user.getAccountbalance() == null ? new BigDecimal("0.00")
					: user.getAccountbalance();

			/** 3.更新用户账户余额 **/
			BigDecimal orderPayAmount = consultOrder.getPrice();
			BigDecimal afterBalance = beforeBalance.subtract(orderPayAmount).setScale(2);
			user.setAccountbalance(afterBalance);
			userMapper.update(user);

			/** 4.保存账户支出流水信息 **/
			AccountRecord accountRecord = new AccountRecord();
			accountRecord.setAmount(orderPayAmount);
			accountRecord.setOrdersn(consultOrder.getOrdersn());
			accountRecord.setChangetype(AppEnum.common.Order_changetype_out.getValue());
			accountRecord.setConsumetype(AppEnum.common.Order_consumetype_phone.getValue());
			accountRecord.setPatientid(user.getId());
			accountRecord.setIncometype(0);
			accountRecord.setTotalamountBefore(beforeBalance);
			accountRecord.setTotalamountAfter(afterBalance);
			accountRecord.setCreatetime(DateUtils.currentDatetime());
			accountRecordMapper.insert(accountRecord);
		}

		return result;
	}

	@Override
	public PhoneOrder queryOrderDetailById(Integer id) {
		return phoneOrderMapper.selectOne(id);
	}

 
	@Override
	public PhoneOrder queryOrderDetailBySn(String openid, String ordersn) {
		return phoneOrderMapper.queryOrderDetailBySn(openid, ordersn);
	}

 
	@Override
	public PhoneOrder getLastOrderByUserid(Integer userid) {
		return phoneOrderMapper.getLastOrderByUserid(userid);
	}


}
